﻿Imports IETracker.UI.Process.PhoneClassLibrary.IETrackerServiceReference
Partial Public Class TransactionPage
    Inherits PhoneApplicationPage
    Private client As New IETrackerServiceClient
    Private pageIsValid As Boolean = False
    Dim trans As TransactionList

#Region "Page Events"
    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub TransactionPage_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        saveButtonState()
    End Sub

    Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)

        MyBase.OnNavigatedTo(e)
        ' Code to work with the ID
        AddHandler client.GetTransactionDescriptionsCompleted, AddressOf client_GetTransactionDescriptionsCompleted

        If Not State.ContainsKey("WentToDatePicker") Then
            Dim transtype As String = Nothing
            'If e.Uri  e.NavigationMode = NavigationMode.New Then
            trans = App._selectedTransaction
            DataContext = trans
            If Not trans Is Nothing Then
                transtype = trans.TransactionTypeDesc
            Else
                transtype = NavigationContext.QueryString("type")
            End If
            client.GetTransactionDescriptionsAsync(transtype)
        End If
    End Sub

    Protected Overrides Sub OnNavigatedFrom(e As NavigationEventArgs)
        If TypeOf e.Content Is DatePickerPage Then
            State("WentToDatePicker") = True
        End If

        MyBase.OnNavigatedFrom(e)
    End Sub



#End Region

#Region "Support Procedures"
    Private Sub client_GetTransactionDescriptionsCompleted(sender As Object, e As GetTransactionDescriptionsCompletedEventArgs)
        If e.[Error] Is Nothing Then
            AutoCompleteBoxTransaction.ItemsSource = e.Result
        End If
    End Sub

    Sub sortData()
        Dim ied As IEnumerable(Of TransactionList) = From d In App.ViewModel.Items Order By d.TransactionDate Descending
        For Each i In ied
            App.ViewModel.Items.Remove(i)
            App.ViewModel.Items.Add(i)
        Next
    End Sub

    Private Sub client_CreateTransactionCompleted(sender As Object, e As CreateTransactionCompletedEventArgs)
        If e.[Error] Is Nothing Then
            trans = DataContext
            If trans Is Nothing Then
                App.ViewModel.Items.Add(e.Result)
            End If
            sortData()
            NavigationService.GoBack()
        End If
    End Sub

    Sub saveButtonState()
        If AutoCompleteBoxTransaction.Text.Length = 0 Or textBoxAmount.Text.Length = 0 Then
            ButtonSave.IsEnabled = False
        Else
            ButtonSave.IsEnabled = True
        End If
    End Sub
#End Region


#Region "Control Events"
    Private Sub DatePickerTransactionDate_ValueChanged(sender As Object, e As Microsoft.Phone.Controls.DateTimeValueChangedEventArgs) Handles DatePickerTransactionDate.ValueChanged
        DatePickerTransactionDate.Value = e.NewDateTime.ToString
    End Sub

    Private Sub AutoCompleteBoxTransaction_TextChanged(sender As Object, e As System.Windows.RoutedEventArgs) Handles AutoCompleteBoxTransaction.TextChanged
        saveButtonState()
    End Sub

    Private Sub textBoxAmount_TextChanged(sender As Object, e As System.Windows.Controls.TextChangedEventArgs) Handles textBoxAmount.TextChanged
        saveButtonState()
    End Sub

    Private Sub ButtonSave_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles ButtonSave.Click
        Try
            Dim trans As New TransactionList
            If me.DataContext Is Nothing Then
                Dim resultTryParse As Boolean
                If Not Double.TryParse(textBoxAmount.Text, resultTryParse) Then
                    Throw New Exception("Not a valid amount.")
                End If
                trans.TransactionDescription = AutoCompleteBoxTransaction.Text
                trans.TransactionDate = DatePickerTransactionDate.Value.ToString
                trans.TransactionAmount = Convert.ToDecimal(textBoxAmount.Text)
                trans.TransactionComment = textBoxComments.Text
                trans.TransactionTypeDesc = NavigationContext.QueryString("type").ToString
            Else
                trans = DataContext
            End If
        AddHandler client.CreateTransactionCompleted, AddressOf client_CreateTransactionCompleted
            client.CreateTransactionAsync(trans)
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString)
        End Try
    End Sub

#End Region




   
End Class
